/*
 * @Author: 王明明834578987 10266004+wang-mingming-834578987@user.noreply.gitee.com
 * @Date: 2024-08-29 10:59:21
 * @LastEditors: 王明明834578987 10266004+wang-mingming-834578987@user.noreply.gitee.com
 * @LastEditTime: 2024-10-14 14:15:49
 * @FilePath: \ft-apply-admin\src\main.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import 'uno.css'
import 'ant-design-vue/dist/reset.css'
import './design/index.less'
// Register icon sprite
import 'virtual:svg-icons-register'
import { createApp } from 'vue'
import App from './App.vue'
import { initAppConfigStore } from '@/logics/initAppConfig'
import { setupErrorHandle } from '@/logics/error-handle'
import { router, setupRouter } from '@/router'
import { setupRouterGuard } from '@/router/guard'
import { setupStore } from '@/store'
import { setupGlobDirectives } from '@/directives'
import { setupI18n } from '@/locales/setupI18n'
import { registerGlobComp } from '@/components/registerGlobComp'
import '@/utils/tongji'
import * as antd from "@ant-design/icons-vue";
import '@/assets/iconfont/iconfont.css';
import 'animate.css';
import { createFromIconfontCN } from '@ant-design/icons-vue';
const IconFont = createFromIconfontCN({
  scriptUrl: '//at.alicdn.com/t/c/font_4616398_e7mrzpceunn.js',
});


async function bootstrap() {
  const app = createApp(App)
  app.component('IconFont', IconFont);
  // Configure store
  // 配置 store

  setupStore(app)

  // Initialize internal system configuration
  // 初始化内部系统配置
  initAppConfigStore()

  // Register global components
  // 注册全局组件
  registerGlobComp(app)

  // Multilingual configuration
  // 多语言配置
  await setupI18n(app)

  // Asynchronous case: language files may be obtained from the server side
  // 异步案例：语言文件可能从服务器端获取
  // Configure routing
  // 配置路由
  setupRouter(app)

  // router-guard
  // 路由守卫
  setupRouterGuard(router)


  // Register global directive
  // 注册全局指令
  setupGlobDirectives(app)


  // Configure global error handling
  // 配置全局错误处理
  setupErrorHandle(app)

  // https://next.router.vuejs.org/api/#isready
  // await router.isReady();
  for (const [key, component] of Object.entries(antd)) {
    app.component(key, component)
  }

  app.config.warnHandler = () => null
  app.mount('#app')

}
// 检查是否是最新的代码
let lastModified = '';
async function checkForUpdates() {
  try {
    const {VITE_PUBLIC_PATH,VITE_USER_NODE_ENV} =import.meta.env;
    if(VITE_USER_NODE_ENV ==="development") return
    const response = await fetch(VITE_PUBLIC_PATH+'/index.html'+"?"+Math.random(), { method: 'HEAD' });
    const newLastModified = response.headers.get('Last-Modified');
    if (newLastModified && lastModified !== newLastModified) {
      if (!lastModified) {
        lastModified = newLastModified;
      } else {
        lastModified = newLastModified;
        notifyUserToReload();
      }
    }
  } catch (error) {
    console.error('Error checking for updates:', error);
  }
}
function notifyUserToReload() {
  const updateBanner = document.createElement('div');
  updateBanner.innerText = '新版本已发布，点击刷新以获得最新内容。';
  updateBanner.style.position = 'fixed';
  updateBanner.style.bottom = '0';
  updateBanner.style.zIndex = '9999';
  updateBanner.style.width = '100%';
  updateBanner.style.backgroundColor = 'yellow';
  updateBanner.style.textAlign = 'center';
  updateBanner.style.padding = '10px';
  updateBanner.style.cursor = 'pointer';
  updateBanner.onclick = () => {
    window.location.reload();
  };
  document.body.appendChild(updateBanner);
}

// 每隔30s检查一次版本更新
setInterval(checkForUpdates, 30000);

bootstrap()
